/*
 * {comment of class}
 *
 * @author (C) <a href="http://www.carfield.com.hk">Carfield Yim </a>, 2004
 */
package hk.com.carfield.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Random;

public class TemplateUtil {
	public static String escape(final String str) throws UnsupportedEncodingException {
		return StringUtil.notBlank(str) ? URLEncoder.encode(str, "UTF-8") : str;
	}
	
	public static String trimParameters(final String str) {
		if(str == null) return "";
		final int qIndex = str.indexOf('?');
		return qIndex > 0 ? str.substring(0, qIndex):str;
	}
	
	public static String trimExtension(final String fileName) {
		if(fileName == null) return "";
		final int extIndex = fileName.lastIndexOf('.');
		if (extIndex > 0 && fileName.length() > extIndex + 1) {
			return StringUtil.safeSubString(fileName, 0, extIndex);
		}
		return fileName;
	}

	private static final Random rnd = new Random();
	private int[] captchas = new int[2];
    public TemplateUtil() {
    		captchas[0] = rnd.nextInt(10);
    		captchas[1] = rnd.nextInt(10);
	}

	public final int captchas(int i) {
        return captchas[i];
	}
}
